// 国际化
import zh_CN from 'ant-design-vue/lib/locale-provider/zh_CN'
const appMixin = {
  data() {
    return {
      locale: zh_CN, // ant-design-vue 默认国际化初始为简体中文
      isRouterAlive: true
    }
  },
  computed: {
    changeStyle () {
      return { minWidth:this.$config.layout.minWidth, minHeight:this.$config.layout.minHeight }
    }
  },
  created() {
    // vuex和session中数据置换
    this.storeAndSession()
    //在页面刷新时将vuex里的信息保存到sessionStorage里
    window.addEventListener('beforeunload', () => {
      sessionStorage.setItem('store', JSON.stringify(this.$store.state))
    })
  },
  methods: {
    /**
     * vuex和session中数据置换
     */
    storeAndSession () {
      // 获取session中储存的vuex值
      const store = sessionStorage.getItem('store')
      if (store){
        // vuex值替换
        this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(store)))
        // 设置i18n的值
        this.$i18n.locale = this.$store.getters.getLanguage
        // 用完之后session清除
        window.sessionStorage.removeItem('store')
      }
    },
    /**
     * 刷新路由页面
     */
    async reload () {
      this.isRouterAlive = false
      await this.$nextTick(() => {
        this.isRouterAlive = true
      })
    },
    /**
     * 去到首页或主页
     */
    async toIndex () {
      await this.$router.push({ name: 'index' })
    }
  },
  provide() {
    return{
      reload: this.reload,
      toIndex: this.toIndex
    }
  }
}

export {
  appMixin
}
